package Test.code;

import java.io.File;
import java.io.FileWriter;
import Test.tools.Utils;

public class ServiceGener {

	public static void serviceTest(String tableName,String tableBean,String currentBao) throws Exception{
		 String rn = "\r\n";
		 String []array = Utils.stringToArray(tableBean);
		 String src = 
			 
			 "package " + currentBao + ".service;" + rn +
			 rn +
		     "import java.util.List;" + rn +
		     "import java.util.Map;" + rn +
		     "import org.springframework.beans.factory.annotation.Autowired;" + rn +
		     "import org.springframework.stereotype.Service;" + rn +
		     "import " + currentBao + ".bean."+Utils.upperFirstChar(tableName)+ ";" + rn +
		     "import  " + currentBao + ".dao."+Utils.upperFirstChar(tableName)+ "Dao;" + rn +
		     rn +
		     "@Service" + rn +
		     " public class "+Utils.upperFirstChar(tableName)+"Service {" + rn +
		     rn + 
		     rn + 
		     "   @Autowired" + rn +
			 "   "+Utils.upperFirstChar(tableName)+"Dao dao;" + rn +
			 rn + 
			 rn + 
			"   public List<"+Utils.upperFirstChar(tableName)+"> all"+Utils.upperFirstChar(tableName)+"(Map<Object,Object> map){" + rn +
			"	     return dao.all"+Utils.upperFirstChar(tableName)+"(map);" + rn +
			"   }" + rn +
			rn + 
			"   public void del"+Utils.upperFirstChar(tableName)+"(int "+array[0]+"){" + rn +
			"	   dao.del"+Utils.upperFirstChar(tableName)+"("+array[0]+");" + rn +
			"   }" + rn +
			rn + 
			"   public "+Utils.upperFirstChar(tableName)+" "+tableName+"(int "+array[0]+"){" + rn +
			"	   return dao."+tableName+"("+array[0]+");" + rn +
			"   }" + rn +
			rn +
			"   public void update"+Utils.upperFirstChar(tableName)+"("+Utils.upperFirstChar(tableName)+" "+tableName+"){" + rn +
			"	   dao.update"+Utils.upperFirstChar(tableName)+"("+tableName+");" + rn +
			"   }" + rn +
			rn +
			"   public void add"+Utils.upperFirstChar(tableName)+"("+Utils.upperFirstChar(tableName)+" "+tableName+"){" + rn +
			"   	dao.add"+Utils.upperFirstChar(tableName)+"("+tableName+");" + rn +
			"   }" + rn +
			rn +	
			
			
			"	public int count"+Utils.upperFirstChar(tableName)+"(){"+ rn +
			"		return dao.count"+Utils.upperFirstChar(tableName)+"();"  + rn + 
		    "	}"+ rn +
			
			"}"+ rn ;
			
			
		    String path  = System.getProperty("user.dir")+"/src/"+Utils.docToBackslash(currentBao)+"/service/";
			File fpath = new File(path);
			if (!fpath.exists()) {
			   fpath.mkdirs();
		    }
			String fileName = System.getProperty("user.dir")+"/src/"+Utils.docToBackslash(currentBao)+"/service/"+Utils.upperFirstChar(tableName)+"Service.java";
			File f = new File(fileName);
			FileWriter fw = new FileWriter(f);
			fw.write(src);
			fw.flush();
			fw.close();
	 }

	public static void serviceTest_(String tableName,String firstTable , String tableBean,String currentBao) throws Exception{
		 String rn = "\r\n";
		 String []array = Utils.stringToArray(tableBean);
		 String src = 
			 
			 "package " + currentBao + ".service;" + rn +
			 rn +
		     "import java.util.List;" + rn +
		     "import org.springframework.beans.factory.annotation.Autowired;" + rn +
		     "import org.springframework.stereotype.Service;" + rn +
		     "import " + currentBao + ".bean."+Utils.upperFirstChar(tableName)+ ";" + rn +
		     "import  " + currentBao + ".dao."+Utils.upperFirstChar(tableName)+ "Dao;" + rn +
		     rn +
		     "@Service" + rn +
		     " public class "+Utils.upperFirstChar(tableName)+"Service {" + rn +
		     rn + 
		     rn + 
		     "   @Autowired" + rn +
			 "   "+Utils.upperFirstChar(tableName)+"Dao dao;" + rn +
			 rn + 
			 rn + 
			"   public List<"+Utils.upperFirstChar(tableName)+"> all"+Utils.upperFirstChar(tableName)+"(String "+firstTable+"Id){" + rn +
			"	     return dao.all"+Utils.upperFirstChar(tableName)+"("+firstTable+"Id);" + rn +
			"   }" + rn +
			rn + 
			"   public void del"+Utils.upperFirstChar(tableName)+"(int "+array[0]+"){" + rn +
			"	   dao.del"+Utils.upperFirstChar(tableName)+"("+array[0]+");" + rn +
			"   }" + rn +
			rn + 
			"   public "+Utils.upperFirstChar(tableName)+" "+tableName+"(int "+array[0]+"){" + rn +
			"	   return dao."+tableName+"("+array[0]+");" + rn +
			"   }" + rn +
			rn +
			"   public void update"+Utils.upperFirstChar(tableName)+"("+Utils.upperFirstChar(tableName)+" "+tableName+"){" + rn +
			"	   dao.update"+Utils.upperFirstChar(tableName)+"("+tableName+");" + rn +
			"   }" + rn +
			rn +
			"   public void add"+Utils.upperFirstChar(tableName)+"("+Utils.upperFirstChar(tableName)+" "+tableName+"){" + rn +
			"   	dao.add"+Utils.upperFirstChar(tableName)+"("+tableName+");" + rn +
			"   }" + rn +
			rn +	
			"}"+ rn ;
			
			
		    String path  = System.getProperty("user.dir")+"/src/"+Utils.docToBackslash(currentBao)+"/service/";
			File fpath = new File(path);
			if (!fpath.exists()) {
			   fpath.mkdirs();
		    }
			String fileName = System.getProperty("user.dir")+"/src/"+Utils.docToBackslash(currentBao)+"/service/"+Utils.upperFirstChar(tableName)+"Service.java";
			File f = new File(fileName);
			FileWriter fw = new FileWriter(f);
			fw.write(src);
			fw.flush();
			fw.close();
	 }
	
	
}
